package com.wenya.counter.ui.main;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.wenya.counter.R;
import com.wenya.counter.data.bean.ScriptureBean;
import com.wenya.counter.data.db.CounterDatabase;
import com.wenya.counter.databinding.ScriptureItemBinding;

import java.util.ArrayList;
import java.util.List;

public class ScriptureAdapter extends RecyclerView.Adapter<ScriptureAdapter.ScriptureViewHolder> {
    private final List<ScriptureBean> listData = new ArrayList<>();
    private final MainViewModel mainViewModel;
    private boolean isEdita = false;

    public ScriptureAdapter(MainViewModel mainViewModel) {
        this.mainViewModel = mainViewModel;
    }

    public void setData(List<ScriptureBean> listData) {
        this.listData.clear();
        if (listData != null) {
            this.listData.addAll(listData);
        }
        notifyDataSetChanged();
    }

    public void setEdita(boolean edita) {
        isEdita = edita;
        notifyDataSetChanged();
    }

    public void checkAll(boolean checked) {
        for (ScriptureBean item : listData) {
            item.setChecked(checked);
        }
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ScriptureViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ScriptureItemBinding scriptureItemBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.scripture_item, parent, false);
        return new ScriptureViewHolder(scriptureItemBinding);
    }


    @Override
    public void onBindViewHolder(@NonNull ScriptureViewHolder holder, int position) {
        ScriptureBean scriptureBean = listData.get(position);
        holder.scriptureItemBinding.name.setText(scriptureBean.getName());
        setInfo(holder, scriptureBean);
        holder.scriptureItemBinding.cutButton.setOnClickListener(view -> {
            scriptureBean.setTimes(scriptureBean.getTimes() + 1);
            CounterDatabase.getInstance().basicDataDao().update(scriptureBean.getName(), scriptureBean.getTimes());
            setInfo(holder, scriptureBean);
            mainViewModel.cutHoseNumber(listData);
        });
        holder.scriptureItemBinding.checkboxGroup.setVisibility(isEdita ? View.VISIBLE : View.GONE);
        holder.scriptureItemBinding.checkbox.setChecked(scriptureBean.isChecked());
        holder.scriptureItemBinding.checkbox.setOnCheckedChangeListener((buttonView, isChecked) -> {
            scriptureBean.setChecked(isChecked);
        });
        holder.scriptureItemBinding.itemRoot.setOnLongClickListener(v -> {
            final EditText inputServer = new EditText(v.getContext());
            inputServer.setInputType(InputType.TYPE_CLASS_NUMBER);
            AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
            builder.setTitle("请输入 \"" + scriptureBean.getName() + "\" 的总遍数:").setView(inputServer)
                    .setNegativeButton(R.string.cancel, null);
            builder.setPositiveButton(R.string.ok, (dialog, which) -> {
                int totalSize;
                try {
                    totalSize = Integer.parseInt(inputServer.getText().toString());
                } catch (Exception e) {
                    Toast.makeText(inputServer.getContext(), R.string.changeError, Toast.LENGTH_SHORT).show();
                    return;
                }
                scriptureBean.setTimes(totalSize);
                CounterDatabase.getInstance().basicDataDao().update(scriptureBean.getName(), scriptureBean.getTimes());
                setInfo(holder, scriptureBean);
                mainViewModel.cutHoseNumber(listData);
            });
            builder.show();
            inputServer.setText(String.valueOf(scriptureBean.getTimes()));
            return false;
        });
    }

    @SuppressLint("SetTextI18n")
    private void setInfo(ScriptureViewHolder holder, ScriptureBean scriptureBean) {
        holder.scriptureItemBinding.numberInfo.setText(holder.context.getString(R.string.total) + scriptureBean.getTimes() + ";   "
                + holder.context.getString(R.string.single) + (scriptureBean.getTimes() % scriptureBean.getDivisor()) + "/" + scriptureBean.getDivisor() + ";\r\n"
                + holder.context.getString(R.string.sheets) + (scriptureBean.getTimes() / scriptureBean.getDivisor())
        );
    }

    @Override
    public int getItemCount() {
        return listData.size();
    }

    public static class ScriptureViewHolder extends RecyclerView.ViewHolder {
        ScriptureItemBinding scriptureItemBinding;
        Context context;

        public ScriptureViewHolder(@NonNull ScriptureItemBinding scriptureItemBinding) {
            super(scriptureItemBinding.getRoot());
            this.scriptureItemBinding = scriptureItemBinding;
            this.context = scriptureItemBinding.getRoot().getContext();
        }
    }
}
